﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SamSoft.Controls
{
    public class ListBoxCheckBoxItem : ListBoxItem
    {
        /// <summary>
        /// La checkbox interne
        /// </summary>

        CheckBox checkBox = null;

        /// <summary>
        /// Constructeur
        /// </summary>

        public ListBoxCheckBoxItem()
        {
            DefaultStyleKey = typeof(ListBoxCheckBoxItem);
        }

        /// <summary>
        /// Application de l'item
        /// </summary>

        public override void OnApplyTemplate()
        {
            checkBox = this.GetTemplateChild("CheckBox") as CheckBox;

            checkBox.Click += new RoutedEventHandler(checkBox_Click);

            checkBox.IsChecked = this.IsChecked;

            if (this.ShowCheckBox == false)
            {
                HideCheckboxState(false);
            }
            else
            {
                ShowCheckboxState(false);
            }

            base.OnApplyTemplate();
        }

        /// <summary>
        /// Changement
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        void checkBox_Click(object sender, RoutedEventArgs e)
        {
            this.IsChecked = checkBox.IsChecked.Value;
        }

        /// <summary>
        /// Affichage
        /// </summary>

        private bool ShowCheckboxState(bool useTransition = true)
        {
            bool result = VisualStateManager.GoToState(this, "ShowCheck", true);

            return result;
        }

        /// <summary>
        /// Affichage
        /// </summary>

        private bool HideCheckboxState(bool useTransition = true)
        {
            bool result = VisualStateManager.GoToState(this, "HideCheck", useTransition);

            return result;
        }

        /// <summary>
        /// Style de la checkbox
        /// </summary>

        public Style CheckBoxStyle
        {
            get { return (Style)GetValue(CheckBoxStyleProperty); }
            set { SetValue(CheckBoxStyleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CheckBoxStyle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CheckBoxStyleProperty =
            DependencyProperty.Register("CheckBoxStyle", typeof(Style), typeof(ListBoxCheckBoxItem), new PropertyMetadata(null));

        /// <summary>
        /// Est checké ?
        /// </summary>

        public bool IsChecked
        {
            get { return (bool)GetValue(IsCheckedProperty); }
            set { SetValue(IsCheckedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsChecked.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsCheckedProperty =
            DependencyProperty.Register("IsChecked", typeof(bool), typeof(ListBoxCheckBoxItem), new PropertyMetadata(false, OnIsCheckedChanged));

        /// <summary>
        /// Changement
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private static void OnIsCheckedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ListBoxCheckBoxItem item = sender as ListBoxCheckBoxItem;

            if (item.checkBox != null)
            {
                item.checkBox.IsChecked = (bool)e.NewValue;
            }

            item.OnChecked();
        }

        /// <summary>
        /// OnChecked
        /// </summary>

        protected virtual void OnChecked()
        {
            if (this.Checked != null)
            {
                this.Checked(this, null);
            }
        }

        public event EventHandler Checked = null;

        /// <summary>
        /// Affichage des Checkbox
        /// </summary>

        public bool ShowCheckBox
        {
            get { return (bool)GetValue(ShowCheckBoxProperty); }
            set { SetValue(ShowCheckBoxProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ShowCheckBox.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ShowCheckBoxProperty =
            DependencyProperty.Register("ShowCheckBox", typeof(bool), typeof(ListBoxCheckBoxItem), new PropertyMetadata(false, OnShowCheckBoxChanged));

        /// <summary>
        /// Changement dans l'affichage
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private static void OnShowCheckBoxChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ListBoxCheckBoxItem item = sender as ListBoxCheckBoxItem;

            if ((bool)e.NewValue == true)
            {
                item.ShowCheckboxState();
            }
            else
            {
                item.HideCheckboxState();
            }
        }
    }
}
    